package com.yjz.demo.thread;

import android.os.Bundle;
import android.view.View;

import com.yjz.common.base.BaseActivity;
import com.yjz.common.thread.YThreadPool;
import com.yjz.log.JLog;
import com.yjz.demo.R;

/**
 * 作用 : 自定义恢复暂停的线程池
 * 作者 : yjz
 * 时间 : 2025/2/8 14:17
 */
public class CustomThreadPoolActivity extends BaseActivity {

    boolean isPause = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_thread;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);

        findViewById(R.id.btn_start_thread).setOnClickListener(v -> {
            for (int i = 0; i < 10; i++) {
                int finalI = i;
                YThreadPool.getInstance().execute(i, new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000 - finalI * 100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

        findViewById(R.id.btn_break_thread).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isPause = !isPause;
                if (isPause) {
                    YThreadPool.getInstance().pause();
                } else {
                    YThreadPool.getInstance().resume();
                }
            }
        });

        findViewById(R.id.btn_result_get).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YThreadPool.getInstance().execute(1, new YThreadPool.Callable<String>() {

                    @Override
                    public String onBackground() {
                        return "我是异步任务结果";
                    }

                    @Override
                    public void onCompleted(String s) {
                        JLog.e("thread", "异步任务结果是：" + s);
                    }
                });
            }
        });
    }
}
